﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Log;
using CaseManagerLibrary.Models;
using Newtonsoft.Json;
using PSDClientService;
using PSDClientService.SystemSettings;
using PSDLib;
using PSDLib.Models;
using PSDManager.SystemSettings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDStorageTool
{
    public class PSDStorageToolContext
    {
        private string deviceId;
        private dl_download_device device;
        System.ServiceProcess.ServiceController serviceController = null;

        public void Init()
        {
            if (PSDClientService.SystemSettings.AppSettings.Enabled)
            {
                FrmLogin login = new FrmLogin();
                if (login.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    deviceId = DeviceUUID.GetUUID();

                    var context = new ClientServiceContext();
                    context.Logger = new ConsoleLog();
                    device = context.DeviceManager.GetDeviceRegistInfo(deviceId);

                    if (device != null)
                    {
                        FrmPSDDevice frmDevice = new FrmPSDDevice();
                        frmDevice.StartPosition = FormStartPosition.CenterParent;
                        frmDevice.SetPSD(device);
                        frmDevice.btnResetStorage.Visible = true;

                        if (frmDevice.ShowDialog() == DialogResult.OK)
                        {
                            var result = HttpHelper.Post<HResult>(PSDManager.SystemSettings.AppSettings.Setting.API.RegistDownloadDevice, JsonConvert.SerializeObject(device));

                            if (result.success)
                            {
                                var p = System.Diagnostics.Process.GetProcessesByName("PSDClientService");
                                if (p.Length > 0)
                                {
                                    p[0].Kill();

                                    System.Threading.Thread.Sleep(1000 * 2);
                                }

                                serviceController = new System.ServiceProcess.ServiceController("PSDClientService");

                                serviceController.Refresh();

                                if (serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                                {
                                    serviceController.Start();
                                    serviceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
                                }

                                serviceController.Close();
                                serviceController.Dispose();
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("未找到配置文件");
                }
            }
        }
    }
}
